<?php 


require ("../lib/upload.php");
require ("../lib/imaging.php");
if($_GET['delfile']==1){
	deleteimage($_GET['image'],$_GET['normal_image'],$_GET['small_image'])	;
}
if($_FILES['file']){
	$x=uploadimage123('file');	
	$name=$x['small_image'];
	echo '{"name":"<input type=\'hidden\' name=\'small_image\' value=\''.$x['small_image'].'\' id=\'small_image\' /><input type=\'hidden\' name=\'normal_image\' value=\''.$x['normal_image'].'\' id=\'normal_image\'/><input type=\'hidden\' name=\'image\' value=\''.$x['image'].'\' id=\'image\' /><img style=\'float:left\' src=\'../upload/images/'.$name.'\'/>'.$xxxx.'"}';
}

function uploadimage123($inputname,$thumnail='yes',$dich=''){
	global $CONFIG,$_FILES,$tpl,$category,$par_page,$_GET;
	$a=array();
	if($dich==''){
		$dich=$CONFIG['root_path'].$CONFIG['upload_image_path'];
	}else {
		$dich=$dich;
	}
	if($category==''){
		$category=$par_page;
	}
	if ($_FILES[$inputname]['size']){
		$in_image=get_new_file_name($_FILES[$inputname]['name'],clean_url(get_file_name($_FILES[$inputname]['name'])));
		$file_upload=new Upload($dich,'jpg,gif,png,bmp,swf');
		if ($file_upload->upload_file($inputname,2,$in_image))
		{
			//Da upload thanh cong
			//Tao thumbnail
			$a['image']=$file_upload->file_name;
			if($CONFIG['watermark_image']){
				$watermark = WideImage::load($CONFIG['root_path'].$CONFIG['upload_image_path'].$CONFIG['watermark_image']);
				$base = WideImage::load($dich.$a['image']);
				$result = $base->merge($watermark, "right - 10", "bottom - 10", 100);
			    $result->saveToFile($dich.$a['image']);
			}elseif($CONFIG['watermark_text']){
				 $img = WideImage::load($dich.$a['image']);
				 $canvas = $img->getCanvas();
				
				 $canvas->useFont('VARISON.TTF', 13, $img->allocateColor(255, 255, 255));
				 $canvas->writeText("right - 9", "top + 11", $CONFIG['watermark_text']);
				 $canvas->useFont('VARISON.TTF', 13, $img->allocateColor(9, 171, 25));
				 $canvas->writeText("right - 10", "top + 10", $CONFIG['watermark_text']);
				 $img->saveToFile($dich.$a['image']);	
				
			}
		  	deleteimage($dich.$a['image']);
			
				$thumbnail=create_thumb($dich, $file_upload->file_name);
				if ($thumbnail)
				{
					$a['small_image']=$thumbnail['thumb'];
					$a['normal_image']=$thumbnail['normal'];
				}
				else
				{
					$msg.="Kh&#244;ng t&#7841;o &#273;&#432;&#7907;c &#7843;nh thumbnail ! Xem l&#7841;i &#273;&#7883;nh d&#7841;ng file !<br>";
				}
			
			
		}
		else
		{
			$msg.=$file_upload->get_upload_errors()."<br>";
		}			
	}
	return $a;
}



//echo '{'.$file['small_image'].'}';
?>



